package domain.gbshaasrode.spreadsheet;

import java.io.IOException;
import java.net.URL;

import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.ServiceException;

import domain.DomainException;

public abstract class GBSOpvangWorkSheet {
	
	private GBSHaasrodeSpreadSheetService gbsService;
	private GBSOpvangSpreadSheet gbsOpvangSpreadSheet;
	
	private WorksheetEntry worksheet;
	
	public GBSOpvangWorkSheet(GBSHaasrodeSpreadSheetService service, GBSOpvangSpreadSheet gbsOpvangSpreadSheet) throws DomainException{
		setGbsService(service);
		setGbsOpvangSpreadSheet(gbsOpvangSpreadSheet);
		initWorkSheet();
	}

	protected abstract void initWorkSheet() throws DomainException;

	public GBSHaasrodeSpreadSheetService getGbsService() {
		return gbsService;
	}

	private void setGbsService(GBSHaasrodeSpreadSheetService gbsService) {
		this.gbsService = gbsService;
	}

	public GBSOpvangSpreadSheet getGbsOpvangSpreadSheet() {
		return gbsOpvangSpreadSheet;
	}

	private void setGbsOpvangSpreadSheet(GBSOpvangSpreadSheet gbsOpvangSpreadSheet) {
		this.gbsOpvangSpreadSheet = gbsOpvangSpreadSheet;
	}

	public WorksheetEntry getWorksheet() {
		return worksheet;
	}

	protected void setWorksheet(WorksheetEntry worksheet) {
		this.worksheet = worksheet;
	}
	
	@Override
	public String toString(){
		String overview = "";
		URL listFeedUrl = getWorksheet().getListFeedUrl();
	    ListFeed listFeed;
		try {
			listFeed = getGbsService().getService().getFeed(listFeedUrl, ListFeed.class);
			// Iterate through each row, printing its cell values.
		    for (ListEntry row : listFeed.getEntries()) {
		      // Print the first column's cell value
		    	String rownr = row.getTitle().getPlainText();
		    	overview += (rownr + "\t");
		      // Iterate over the remaining columns, and print each cell value
		      for (String tag : row.getCustomElements().getTags()) {
		    	  overview += (row.getCustomElements().getValue(tag) + "\t");
		      }
		      overview += "\n";
		     }	
		} catch (IOException | ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	    	
	    return overview;
	}

}
